﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace Lottery.Client.SL.Controls
{
    public partial class LotteryControlInfo : UserControl
    {
        private Canvas _canvas1;

        public LotteryControlInfo()
        {
            InitializeComponent();
            InitLotteryInfo();
        }

        public ISystemMain IMain { get; set; }

        public void InitLotteryInfo()
        {
            var stringBuilder = new StringBuilder();
            for (int i = 0; i < CulbViewMode.GameKind.Games.Count; i++)
            {
                stringBuilder.Append(CulbViewMode.GameKind.Games[i].GameName + "?");
            }
            using (IEnumerator<UIElement> enumerator = ImageButtonCanvas.Children.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    UIElement current = enumerator.Current;
                    if (current is Canvas)
                    {
                        var canvas = (Canvas) current;
                        var textBlock = (TextBlock) canvas.Children[2];
                        if (stringBuilder.ToString().Contains(textBlock.Text))
                        {
                            //每个彩种画布元素点击
                            current.MouseLeftButtonDown += (LotteryEleCanvasLeftMouseDown);
                            //current.MouseEnter+=(new MouseEventHandler(this.dLhBHcu518));
                            //current.MouseLeave+=(new MouseEventHandler(this.RexBrSStBu));
                            if (string.IsNullOrEmpty(DataCache.CurrentPlayInfo.LotteryName))
                            {
                                DataCache.CurrentPlayInfo.LotteryName = textBlock.Text;
                            }
                            if (DataCache.CurrentPlayInfo.LotteryName == textBlock.Text)
                            {
                                stySwitch.RepeatBehavior = (RepeatBehavior.Forever);
                                stySwitch.AutoReverse = true;
                                stySwitch.Begin();
                                _canvas1 = canvas;
                                ChangeTbStyle(canvas);
                            }
                        }
                        else
                        {
                            canvas.Children.Clear();
                        }
                    }
                }
            }
        }

        private void LotteryEleCanvasLeftMouseDown(object obj, MouseButtonEventArgs mouseButtonEventArgs)
        {
            try
            {
                bool flag = false;
                var canvas = (Canvas) obj;
                var textBlock = (TextBlock) canvas.Children[2];
                for (int i = 0; i < CulbViewMode.GameKind.Games.Count; i++)
                {
                    if (CulbViewMode.GameKind.Games[i].GameName == textBlock.Text)
                    {
                        IMain.LoadBuyForm();
                        CulbViewMode.SelectGame = CulbViewMode.GameKind.Games[i];
                        CulbViewMode.SelectGame.SelectGameMenu = CulbViewMode.SelectGame.GamesMenu[0];
                        CulbViewMode.SelectGame.SelectGameMenu.SelectGameMode =
                            CulbViewMode.SelectGame.SelectGameMenu.GameModes[0];
                        int iD = DataCache.LotteryDatas[i].ID;
                        DataCache.CurrentPlayInfo.LotteryID = iD;
                        DataCache.CurrentPlayInfo.LotteryName = textBlock.Text;
                        DataCache.CurrentPlayInfo.OpenChase = DataCache.LotteryDatas[i].OpenChase;
                        DataCache.CurrentPlayInfo.PlayName =
                            CulbViewMode.SelectGame.SelectGameMenu.GameModes[0].PlayTypeName;
                        DataCache.CurrentPlayInfo.PlayTypeID =
                            CulbViewMode.SelectGame.SelectGameMenu.GameModes[0].PlayTypeID;
                        DataCache.CurrentPlayInfo.iPlayTypeID =
                            Convert.ToInt32(iD.ToString() +
                                            CulbViewMode.SelectGame.SelectGameMenu.GameModes[0].PlayTypeID);
                        DataCache.CurrentPlayInfo.iScaleID =
                            Convert.ToInt32(iD.ToString() + CulbViewMode.SelectGame.GamesMenu[0].TypeID.ToString());
                        BetDataControl.ClearBetData();
                        flag = true;
                        IMain.SetLotteryLabel();
                        break;
                    }
                }
                if (flag)
                {
                    IMain.SetReplicate(true);
                    ChangeTbStyle(canvas);
                }
                else
                {
                    App.ShowMsg("此玩法正在维护中，请稍后再试！");
                }
            }
            catch
            {
                App.ShowMsg("此玩法正在维护中，请稍后再试！");
            }
        }

        private void ChangeTbStyle(Canvas canvas)
        {
            SelectStory.SetValue(Canvas.LeftProperty, double.Parse(canvas.GetValue(Canvas.LeftProperty).ToString()));
            SelectStory.SetValue(Canvas.TopProperty, double.Parse(canvas.GetValue(Canvas.TopProperty).ToString()));
            SelectRect.SetValue(Canvas.LeftProperty, double.Parse(canvas.GetValue(Canvas.LeftProperty).ToString()) - 2.0);
            SelectRect.SetValue(Canvas.TopProperty, double.Parse(canvas.GetValue(Canvas.TopProperty).ToString()) - 2.0);
            var textBlock = (TextBlock) _canvas1.Children[2];
            textBlock.Style = ((Style) base.Resources["tbStyle2"]);
            var textBlock2 = (TextBlock) canvas.Children[2];
            textBlock2.Style = ((Style) base.Resources["tbStyle1"]);
            _canvas1 = canvas;
        }
    }
}